
#include <stdio.h>
#include "wm_hal.h"

#define BUF_LEN 1280
static uint8_t txbuf[BUF_LEN + 2];
static uint32_t total_cnt = 0, err_cnt = 0;

void Error_Handler(void);
static void TxData(void);

int main(void)
{
	SystemClock_Config(CPU_CLK_240M);
	printf("enter main\r\n");
	
	HAL_SSPI_Init(INTERFACE_TYPE_SSPI);
	while (1)
	{
		HAL_Delay(1000);
	}
}

void HAL_SSPI_RxDataCpltCallback(uint8_t *buf)
{
	int i = 0;
	uint32_t len = (buf[0] << 8) | buf[1];
	
	for (i = 0; i < len; i++)
	{
		if (buf[i + 2] != (i % 255))
		{
			printf("err i = %d buf[i+2] = %x\n", i, buf[i + 2]);
			err_cnt += 1;
			break;
		}
	}
	total_cnt += 1;
	if ((total_cnt % 100) == 0)
	{
		printf("total_cnt = %d err_cnt = %d\n", total_cnt, err_cnt);
	}
	TxData();
}

void HAL_SSPI_RxCmdCpltCallback(uint8_t *buf)
{
	printf("rx cmd: ");
	printf("%02X %02X %02X\n", buf[0], buf[1], buf[2]);
	
	if ((buf[0] != 0x5A) || (buf[1] != 0x05) || (buf[2] != 0x00))
	{
		printf("rx cmd err\n");
		return;
	}
	TxData();
}

static void TxData(void)
{
	int i = 0;
	
	txbuf[0] = 0x05;
	txbuf[1] = 0x00;
	for (i = 0; i < BUF_LEN; i++)
	{
		txbuf[i + 2] = i % 255;
	}
	HAL_SSPI_Transmit(txbuf, BUF_LEN + 2);
}

void Error_Handler(void)
{
	while (1)
	{
	}
}

void assert_failed(uint8_t *file, uint32_t line)
{
	printf("Wrong parameters value: file %s on line %d\r\n", file, line);
}